Ext.define("YX.core.Main", {
  extend: "Ext.container.Viewport",
  layout: {
    type: "border",
    split: true,
    splitterResize: true,
  },
  bOpenning: false,
  pelNav: null,
  pelNavBtn: null,
  btnUI: null,
  btnOI: null,
  btnChart: null,
  treeNav: null,
  frmFilterNav: null,
  txtFilter: null,
  pelMain: null,
  pelNavAllMenu: null,
  tabMain: null,
  loadMask: null,
  requires: ["UX.codeview.FieldX"],
  statics: {
    addTab: function (cmp) {
      if (cmp) {
        var main = Ext.ClassManager.get("main");
        var destCmp = main.tabMain.getComponent(main.getItemId());
        if (!destCmp) {
          main.tabMain.add(cmp);
          destCmp = cmp;
        }

        main.tabMain.setActiveTab(destCmp);
      }
    },
  },
  initComponent: function () {
    this.btnUI = Ext.create("Ext.button.Button", {
      scale: "large",
      width: 64,
      icon: "resources/" + appName + "/icon/run-build-2.png",
      style: "margin-left:3px;",
      iconAlign: "top",
      enableToggle: true,
      text: T.label.func,
      scope: this,
      handler: this.onBtnUIClicked,
    });

    this.btnOI = Ext.create("Ext.button.Button", {
      scale: "large",
      width: 64,
      icon: "resources/" + appName + "/icon/player-time-2.png",
      style: "margin-left:3px;",
      iconAlign: "top",
      enableToggle: true,
      text: T.label.operate,
      scope: this,
      handler: this.onBtnOIClicked,
    });

    this.btnChart = Ext.create("Ext.button.Button", {
      scale: "large",
      width: 64,
      icon: "resources/" + appName + "/icon/office-chart-pie.png",
      style: "margin-left:3px;margin-right:3px;",
      iconAlign: "top",
      enableToggle: true,
      text: T.label.chart,
      scope: this,
      handler: this.onBtnChartClicked,
    });

    this.pelNavBtn = Ext.create("Ext.panel.Panel", {
      height: 64,
      layout: {
        type: "hbox",
        align: "stretch",
        pack: "center",
        flex: 1,
      },
      items: [
        {
          flex: 1,
          border: false,
          layout: {
            type: "hbox",
            pack: "center",
          },
          items: [this.btnUI],
        },
        {
          flex: 1,
          border: false,
          layout: {
            type: "hbox",
            pack: "center",
          },
          items: [this.btnOI],
        },
        {
          flex: 1,
          border: false,
          layout: {
            type: "hbox",
            pack: "center",
          },
          items: [this.btnChart],
        },
      ],
    });

    this.treeNav = Ext.create("Ext.tree.Panel", {
      flex: 1,
      rootVisible: false,
      root: {
        expanded: true,
      },
      store: Ext.create("Ext.data.TreeStore", {
        fields: [
          "funCode",
          "parentFunCode",
          "asmName",
          "asmFile",
          "asmClassName",
          "funType",
          "funSide",
          "iconPath",
          "text",
        ],
        proxy: {
          type: "zbusrpc",
          module: "SecViewUserTreeFunctionService",
          showSuccess:false,
          extraParams: {
            funGroup: "EXT",
            funSide: "U",
          },
          reader: {
            type: "json",
          },
        },
      }),
      listeners: {
        itemexpand: this.onTreeNavItemExpand,
      },
    });

    this.pelNav = Ext.create("Ext.panel.Panel", {
      region: "west",
      width: 210,
      split: true,
      title: T.label.systemFunction,
      collapsible: true,
      layout: {
        type: "vbox",
        align: "stretch",
      },
      items: [this.pelNavBtn, this.treeNav],
    });

    this.tabMain = Ext.create("Ext.tab.Panel", {
      itemId: "main",
      items: {
        closable: false,
        title: T.label.home,
        bodyStyle:
          "background-image:url(resources/yxmes/images/laptop-1.png);background-repeat:no-repeat;background-position:center;",
      },
      plugins: Ext.create("Ext.ux.TabCloseMenu", {
        showCloseAll: false,
        extraItemsHead: [
          {
            text: T.label.closeLeft,
          },
          {
            text: T.label.closeRight,
          },
        ],
      }),
    });

    this.pelMain = Ext.create("Ext.panel.Panel", {
      region: "center",
      layout: {
        type: "card",
      },
      items: [
        // 功能操作界面容器
        this.tabMain,
        {
          // 生产操作界面容器
          itemId: "oper",
          xtype: "panel",
          closable: false,
          layout: "fit",
        },
        {
          // 图表界面容器
          itemId: "chart",
          xtype: "panel",
          closable: false,
          layout: "fit",
        },
      ],
    });

    this.pelBottom = new Ext.Panel({
      region: "south",
      height: 25,
      html:
        "<span style='font-size:12px;width:120px;line-height:25px;float:left;'>当前用户：" +
        LoginInfo.USERNAME +
        "</span><span style='position: relative; left:50%;margin-left:-230px;width:300px; height: 25px;font-size:12px;line-height:25px;'>" +
        "深圳微邦软件信息有限公司MES</span>",
    });

    this.items = [this.pelNav, this.pelMain, this.pelBottom];

    this.treeNav.on("itemclick", this.onTreeNavItemClick, this);
    this.on("afterrender", this.onAfterRender, this);
    this.tabMain.on("add", this.onTabMainAdded, this);

    this.loadMask = new Ext.LoadMask(this, {
      msg: T.label.loading,
    });

    this.callParent();
  },
  onAfterRender: function () {
    this.btnUI.toggle(true, true);
  },
  onTreeNavItemClick: function (tree, record, item) {
    if ("F" !== record.get("funType")) {
      return;
    }

    if (this.bOpenning) {
      return;
    }

    var me = this;
    me.bOpenning = true;
    var pelContainer = this.pelMain.getLayout().getActiveItem();
    if (record.get("funSide") == "U") {
      //this.pelMain.getLayout().setActiveItem(0);
      var destCmp = pelContainer.getComponent(record.get("funCode"));
      if (!destCmp) {
        this.genCompFromTreeMenu(record, pelContainer);
      } else {
        pelContainer.setActiveTab(destCmp);
      }
    } else if (record.get("funSide") == "O") {
      pelContainer.removeAll(true);
      this.genCompFromTreeMenu(record, pelContainer);
      //this.pelMain.getLayout().setActiveItem(1);
    } else if (record.get("funSide") == "C") {
      this.genCompFromTreeMenu(record, pelContainer);
      //this.penMain.getLayout().setActiveItem(2);
    }
    me.bOpenning = false;
  },
  genCompFromTreeMenu: function (record, container) {
    var me = this;
    if (!Ext.isEmpty(record.get("asmFile"))) {
      var asmFile = record.get("asmFile").replace("/", ".");
      if (asmFile.toLowerCase().endsWith(".js")) {
        asmFile = asmFile.substring(0, asmFile.lastIndexOf(".js"));
      }
      var path = asmFile.substring(0, asmFile.lastIndexOf(".") + 1);
      asmFile = "YX." + asmFile;
      var className = Ext.String.startsWith(record.get("asmName"), "YX.", true)
        ? record.get("asmName")
        : "YX." + path + record.get("asmName");

      me.loadMask.show();

      Ext.require(asmFile, function () {
        var cmp = Ext.create(className);
        destCmp = Ext.create("Ext.panel.Panel", {
          itemId: record.get("funCode"),
          title: record.get("text"),
          layout: "fit",
          closable: true,
          items: [cmp],
        });
        me.loadMask.hide();
        container.add(destCmp);
        if (container.getXType() === "tabpanel") {
          container.setActiveTab(destCmp);
        }
      });
    } else if (!Ext.isEmpty(record.get("asmClassName"))) {
      var asmClassName = record.get("asmClassName");
      if (
        asmClassName.indexOf("_") > 0 &&
        asmClassName.indexOf("_") === asmClassName.lastIndexOf("_")
      ) {
        asmClassName =
          appName +
          "." +
          asmClassName.substring(0, asmClassName.indexOf("_")) +
          ".view." +
          asmClassName.substr(asmClassName.indexOf("_") + 1);
      }

      // var viewIndex = asmClassName.lastIndexOf("View");
      var ctName = Ext.String.endsWith(asmClassName, "View")
        ? asmClassName.slice(0, -4)
        : asmClassName;
      ctName = ctName.concat("Controller").replace(".view.", ".controller.");

      var ctl = null;
      try {
        ctl = YXMES.getApplication().getController(ctName);
      } catch (ex) {
        me.bOpenning = false;
        Ext.Msg.alert("error", ex);
        throw ex;
      }

      var cmp = ctl
        .getView(
          Ext.String.endsWith(asmClassName, "View")
            ? asmClassName
            : asmClassName.concat("View")
        )
        .create();
      destCmp = Ext.create("Ext.panel.Panel", {
        itemId: record.get("funCode"),
        title: record.get("text"),
        layout: "fit",
        closable: true,
        items: [cmp],
      });

      container.add(destCmp);
      if (container.getXType() === "tabpanel") {
        container.setActiveTab(destCmp);
      }
      // ctl.init();
    }
  },
  onBtnUIClicked: function (btn) {
    this.btnOI.toggle(false, true);
    this.btnChart.toggle(false, true);

    var proxy = this.treeNav.getStore().getProxy();
    proxy.setExtraParam("funSide", "U");
    this.treeNav.getStore().reload();

    this.pelMain.getLayout().setActiveItem("main");
  },
  onBtnOIClicked: function (btn) {
    this.btnUI.toggle(false, true);
    this.btnChart.toggle(false, true);

    var proxy = this.treeNav.getStore().getProxy();
    proxy.setExtraParam("funSide", "O");
    this.treeNav.getStore().reload();

    this.pelMain.getLayout().setActiveItem("oper");
  },
  onBtnChartClicked: function (btn) {
    this.btnOI.toggle(false, true);
    this.btnUI.toggle(false, true);

    var proxy = this.treeNav.getStore().getProxy();
    proxy.setExtraParam("funSide", "C");
    this.treeNav.getStore().reload();

    this.pelMain.getLayout().setActiveItem("chart");
  },
  onTabMainAdded: function (tabPanel, cmp) {
    this.setAuthority(cmp);
  },
  setAuthority: function (cmp) {
    if (cmp == null || LoginInfo.ORGCODE == "SYSTEM") {
      return;
    }

    var buttons = cmp.query("button");
    if (!Ext.isEmpty(buttons)) {
      var ctlBtns = [];
      Ext.each(buttons, function (btn) {
        if (Ext.String.startsWith(btn.itemId, "btn", true)) {
          btn.disable();
          ctlBtns.push(btn);
        }
      });

      if (!Ext.isEmpty(ctlBtns)) {
        YX.ZbusRpc.request({
          module: "SecViewUserFunctionControlService",
          showFail: false,
          showSuccess: false,
          params: {
            funCode: cmp.itemId,
          },
          success: function (response) {
            var ctlList = response.ctlList;
            if (Ext.isEmpty(ctlList)) {
              return;
            }

            Ext.each(ctlBtns, function (btn) {
              Ext.each(ctlList, function (ctl) {
                if (ctl.ctlName === btn.itemId) {
                  btn.setDisabled(ctl.hasCtl !== "Y");
                  return false;
                }
              });
            });
          },
        });
      }
    }
  },
  onTreeNavItemExpand: function (treeNode) {
    if (!Ext.isEmpty(treeNode.childNodes)) {
      var host = window.location.host.toLowerCase();
      Ext.each(treeNode.childNodes, function (cn) {
        var asm = "";
        if (host.indexOf("localhost") == 0 || host.indexOf("127.0.0.1") == 0) {
          asm =
            " " +
            (Ext.isEmpty(cn.get("asmFile"))
              ? cn.get("asmClassName")
              : cn.get("asmFile"));
        }
        cn.set("qtip", cn.get("text") + asm);
      });
    }
  },
});
